package com.input4hua.expr.time.handler.hour;


import static com.input4hua.expr.time.DataHandlerConstants.MINUS;
import static com.input4hua.expr.time.DataHandlerConstants.PLUS;
import static com.input4hua.expr.time.untils.TimeFormatterUtils.TIME_FORMAT_PATTERN_HOUR;

import java.util.regex.Pattern;

import com.input4hua.expr.time.AbstractDateHandler;
import com.input4hua.expr.time.untils.TimeCalcUtils;
import com.input4hua.expr.time.untils.TimeFormatterUtils;

/**
 * 支持的 ${HOUR}  极其相对偏移日期  输出HH
 * @author Jiahua.hu
 * @since 13 May 2019
 */
public class ShortHourExprHandler extends AbstractDateHandler {

	private static final Pattern pattern = Pattern.compile("\\$\\{hour[\\+\\-]?\\d?}");

	@Override
	protected String exprParse(String expr) {
		String resp;
		if (expr.contains(PLUS)) {
			String subStr = expr.substring(expr.indexOf(PLUS) + 1, expr.length() - 1);
			Integer num = Integer.valueOf(subStr);
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.nextHours(num), TIME_FORMAT_PATTERN_HOUR);
		}
		else if (expr.contains(MINUS)) {
			String subStr = expr.substring(expr.indexOf(MINUS) + 1, expr.length() - 1);
			Integer num = Integer.valueOf(subStr);
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.prevHours(num), TIME_FORMAT_PATTERN_HOUR);
		}
		else {
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.now(), TIME_FORMAT_PATTERN_HOUR);
		}
		return resp;
	}

	@Override
	protected Pattern getPattern() {
		return pattern;
	}
}
